package com.lingnan.wanxiangxinxun.controller.admin;


import com.lingnan.wanxiangxinxun.entity.dto.UserDTO;
import com.lingnan.wanxiangxinxun.entity.dto.UserLoginOrRegisterDTO;
import com.lingnan.wanxiangxinxun.entity.dto.query.User_queryDTO;
import com.lingnan.wanxiangxinxun.entity.po.AuthorApplication;
import com.lingnan.wanxiangxinxun.entity.po.User;
import com.lingnan.wanxiangxinxun.entity.vo.common.PageVO;
import com.lingnan.wanxiangxinxun.entity.vo.common.Result;
import com.lingnan.wanxiangxinxun.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author swl
 * @since 2025-03-13
 */
@RestController
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private IUserService userService;


    //新增用户
    @PostMapping("/register")
    public Result registerUser(@RequestBody UserLoginOrRegisterDTO uLOrR_DTO) {
        User user = new User();
        user.setCreateTime(LocalDateTime.now());
        BeanUtils.copyProperties(uLOrR_DTO, user);
        try {
            userService.save(user);
        } catch (Exception e) {
            return Result.error("注册失败");
        }
        return Result.success();
    }


    //删除用户
    @DeleteMapping("/deleteUserByIds/{ids}")
    public Result deleteUser(@PathVariable List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return Result.error("未选中用户！");
        }

        try{
            boolean deleteRows = userService.removeBatchByIds(ids);
            if(deleteRows){
                return Result.success();
            }else{
                return Result.error("注销失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("未知错误");
        }
    }


    //修改用户信息
    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        user.setUpdateTime(LocalDateTime.now());

        try {
            userService.updateById(user);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("修改失败");
        }
        return Result.success();
    }


    //用户查询--分页查所有(按注册时间倒序排)
    @GetMapping("/getAllInPage")
    public Result getUser(@RequestParam(defaultValue = "1") long current,
                          @RequestParam(defaultValue = "10") long size)
    {
        PageVO<User> allInPage = userService.getAllInPage(current, size);
        return Result.success(allInPage);
    }


    //查找--根据条件查
    @PostMapping("/getAllInCondition")
    public Result getUserByCondition(@RequestBody User_queryDTO userDTO) {
        PageVO<User> allInCondition = userService.getInCondition(userDTO);
        return Result.success(allInCondition);
    }


    //查找--根据id查
    @GetMapping("/getById")
    public Result getUserById(@RequestParam List<Integer> ids) {
        List<User> userList = userService.listByIds(ids);
        return Result.success(userList);
    }




    //查询作者申请记录
    @GetMapping("/getAuthorApply")
    public Result getAuthorApply(@RequestParam long current, @RequestParam long size){
        PageVO<AuthorApplication> AuthorApplicationList = userService.getAuthorApply(current, size);
        return Result.success(AuthorApplicationList);
    }


    /**
     * @param id mongodb数据库单个文档的唯一id
     * @param userId mysql数据库的用户id
     * @param type 0:审核未通过 1:审核通过为个人作者  2:审核通过为机构作者
     */
    //审核作者申请
    @PostMapping("/verifyAuthorApply")
    public Result verifyAuthorApply(@RequestParam String id,
                                    @RequestParam String userId,
                                    @RequestParam(defaultValue = "0") Integer type) {
        return userService.verifyAuthorApply(id,userId,type);
    }

}




